package com.xmutca.sso.provider.manager;

import com.alibaba.dubbo.config.annotation.Service;
import com.xmutca.core.common.Paging;
import com.xmutca.core.common.Receipt;
import com.xmutca.core.common.Result;
import com.xmutca.core.common.Results;
import com.xmutca.core.common.exception.ServiceException;
import com.xmutca.sso.api.Menu;
import com.xmutca.sso.api.Permission;
import com.xmutca.sso.api.manager.service.MenuService;
import com.xmutca.sso.dao.MenuDao;
import com.xmutca.sso.dao.PermissionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;
import java.util.Map;

/**
 * @author: 彭伟煌(pengweihuang@xmutca.com)
 * @create: 2016-08-26 17:13
 */
@Service(version = "1.0.0", group = "sso", mock = "true")
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 新增数据
     * @param menu
     * @return
     */
    @Override
    public Receipt add(Menu menu) {
        if (menu.getParent() != null) {
            Menu parent = menuDao.get(menu.getParent());
            if (parent == null) {
                throw new ServiceException(Result.Status.ERROR, "父节点不存在");
            }
        }

        menuDao.add(menu);
        return new Receipt(Receipt.Status.SUCCESS);
    }

    /**
     * 删除数据
     * @param menuId
     * @return
     */
    @Override
    @Transactional
    public Receipt del(Integer menuId) {
        permissionDao.updateStateByMenu(Permission.STATE_UNABLE, menuId);
        menuDao.updateState(Menu.STATE_UNABLE, menuId);
        return new Receipt(Receipt.Status.SUCCESS);
    }

    /**
     * 批量删除
     * @param menuIds
     * @return
     */
    @Override
    @Transactional
    public Receipt batchDel(List<Integer> menuIds) {
        permissionDao.batchUpdateStateByMenu(Permission.STATE_UNABLE, menuIds);//修改权限表状态
        menuDao.batchUpdateState(Menu.STATE_UNABLE, menuIds);
        return new Receipt(Receipt.Status.SUCCESS);
    }

    /**
     * 更新数据
     * @param menu
     * @return
     */
    @Override
    public Receipt update(Menu menu) {
        if (menu.getParent() != null) {
            Menu parent = menuDao.get(menu.getParent());
            if (parent == null) {
                throw new ServiceException(Result.Status.ERROR, "父节点不存在");
            }
        }

        menuDao.update(menu);
        return new Receipt(Receipt.Status.SUCCESS);
    }

    /**
     * 读取数据
     * @param menuId
     * @return
     */
    @Override
    public Result<Menu> get(Integer menuId) {
        return new Result<>(Result.Status.SUCCESS, menuDao.get(menuId));
    }

    /**
     * 查询
     * @param current
     * @param pageSize
     * @param paramMap
     * @return
     */
    @Override
    public Results<Menu> query(@Min(value = 1, message = "页数最小为1") int current, @Max(value = 50, message = "条数最大为50条") int pageSize, Map<String, Object> paramMap) {
        int count = menuDao.countQuery(paramMap);
        paramMap.put("startRow", (current - 1) * pageSize);
        paramMap.put("pageSize", pageSize);
        List<Menu> accounts = menuDao.query(paramMap);
        return new Results<>(Results.Status.SUCCESS, accounts, new Paging(current, pageSize, count));
    }
}
